/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.LocationsBusiness;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Locations;

/**
 *
 * @author Administrator
 */
@ManagedBean
@RequestScoped
public class MngLocationsBean {

    private LocationsBusiness locationsBusiness = new LocationsBusiness();
    private List<Locations> listLocal;
    private Locations local;
    private List<Integer> listPage;

    public List<Integer> getListPage() {
        return listPage;
    }

    public void setListPage(List<Integer> listPage) {
        this.listPage = listPage;
    }

    public Locations getLocal() {
        return local;
    }

    public void setLocal(Locations local) {
        this.local = local;
    }

    public List<Locations> getListLocal() {
        return listLocal;
    }

    public void setListLocal(List<Locations> listLocal) {
        this.listLocal = listLocal;
    }

    public String updateLocalAction(Locations local) {
        locationsBusiness.updateLocation(local);
        listLocal = locationsBusiness.listAllLocation();
        return "managementlocations.xhtml";
    }

    public String addNewLocalAction() {
        locationsBusiness.newLocation(local);
        listLocal = locationsBusiness.listAllLocation();
        return "managementlocations.xhtml";
    }

    /** Creates a new instance of MngLocationsBean */
    public MngLocationsBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        int pageID;
        if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID") != null) {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID").toString());
        } else {
            pageID = Integer.parseInt(session.getAttribute("PageIDLocal").toString());
        }
        session.setAttribute("PageIDLocal", pageID);
        local = new Locations();
        int index = locationsBusiness.getRowInListAllLocation();
        listPage = new ArrayList<Integer>();
        if (index / 3 < 1) {
            listPage.add(1);
        } else {
            if (index % 3 == 0) {
                for (int i = 1; i <= index / 3; i++) {
                    listPage.add(i);
                }
            } else {
                for (int i = 1; i <= index / 3 + 1; i++) {
                    listPage.add(i);
                }
            }
        }
        listLocal = locationsBusiness.listLocationByPage(pageID);
    }
}
